package com.bree.erp.jsf.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.bree.erp.entity.Permission;
import com.bree.erp.entity.PermissionId;
import com.bree.erp.entity.Project;
import com.bree.erp.entity.User;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.jsf.vo.PermissionDT;
import com.bree.erp.service.PermissionService;
import com.bree.erp.service.ProjectService;

@ManagedBean(name="dtGroupPermissionView")
@ViewScoped
public class GroupPermissionView implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@EJB
	private ProjectService projectService;
	
	@EJB
	private PermissionService permissionService;
	
	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;
	
	private List<PermissionDT> groups;
	private User user;
	
	private List<Project> currentUserProjects;
	
	private void initGroups() {
		groups = new ArrayList<PermissionDT>();
		
		if(user != null) {
			List<Project> projects = projectService.list();
			String userId = user.getId();
			
			for(Project project : projects) {
				Long projectId = project.getId();
				
				PermissionId pId = new PermissionId(userId, projectId);
				Permission perm = permissionService.read(pId);
				if(perm == null) {
					perm = permissionService.create(userId, projectId);
				}
				
				PermissionDT group = new PermissionDT(project, perm.getGroups());
				groups.add(group);
			}
			
		}
	}
	
	public void save() {
		
		currentUserProjects = null;
		
		for(PermissionDT group : groups) {
			PermissionId id = new PermissionId(user.getEmail(), group.getProject().getId());
			permissionService.update(id , group.getGroups());
		}
			
	}

	/**
	 * @return the currentUserProjects
	 */
	public List<Project> getCurrentUserProjects() {
		
		if(currentUserProjects == null) {
			currentUserProjects = permissionService.listProjects(userSession.getLoggedInUser().getEmail());
		}
		
		return currentUserProjects;
	}
	

	/**
	 * @return the groups
	 */
	public List<PermissionDT> getGroups() {
		return groups;
	}

	/**
	 * @param groups the groups to set
	 */
	public void setGroups(List<PermissionDT> groups) {
		this.groups = groups;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
		
		initGroups();
	}


	/**
	 * @return the selectedItem
	 */
	public User getSelectedItem() {
		return getUser();
	}


	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(User selectedItem) {
		setUser(selectedItem);
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}
	
	
}
